dnl #############################################################################
dnl #
dnl # Process this file with autoconf then autoheader to build a configure script
dnl #
dnl #############################################################################

AC_PREREQ(2.12)

AC_INIT(src/cfengine.c)
AC_CONFIG_HEADER(src/conf.h)

AC_MSG_WARN("------------------------------------------------")
AC_MSG_WARN(" DFS users should use --enable-DCE")
AC_MSG_WARN(". . . . . . . . . . . . . . . . . . . . . . . . . ")
AC_MSG_WARN("If you are compiling on older pre-POSIX or NT systems")
AC_MSG_WARN("you should get the librx compatibility lib from GNU")
AC_MSG_WARN("------------------------------------------------")

AC_CANONICAL_SYSTEM

cfversion=`cat $srcdir/version`
AC_SUBST(cfversion)

#
# Add to the default list of places in LDFLAGS to compensate for
# ... the configure default value of LIBS on some systems
#
for x in /usr/local/ssl/lib /usr/local/BerkeleyDB/lib /usr/local/gnu/lib \
	/usr/local/gnulib /usr/local/lib 
do
  if test -d "$x"; then
    y=`expr " $LDFLAGS " : ".* -L$x "`
    if test $y -eq 0; then
      LDFLAGS="$LDFLAGS -L$x"
    fi
  fi
done

#
# Add to the default list of places in CPPFLAGS to match LDFLAGS above
# 
for x in /usr/local/ssl/include /usr/local/BerkeleyDB/include \
	/usr/local/include /usr/local/gnu/include
do 
  if test -d "$x"; then
    y=`expr " $CPPFLAGS " : ".* -I$x "`
    if test $y -eq 0; then
      CPPFLAGS="$CPPFLAGS -I$x"
    fi
  fi
done

dnl ######################################################################
dnl Checks for programs.
dnl ######################################################################

AC_PROG_MAKE_SET

AC_PROG_CC
ADD_CC_WALL=false	dnl Is this needed?  Let autoconf decide?

AC_PROG_LEX
AC_PROG_YACC

AC_PROG_RANLIB
AC_PROG_INSTALL

AC_PATH_PROG(PERL, perl, /bin/false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(TEX, tex, /bin/false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(LATEX, latex, /bin/false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(TEXINDEX, texindex, /bin/false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(DVIPS, dvips, /bin/false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(TEXI2HTML, texi2html, /bin/false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_PATH_PROG(MAKEINFO, makeinfo, /bin/false, \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)

dnl #######################################################################
dnl Determine what documentation to build by looking for builders
dnl #######################################################################

AC_CHECK_PROG(DFILE1, tex, "cfengine-Tutorial.dvi", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(DFILE2, tex, "cfengine-Reference.dvi", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(PFILE1, dvips, "cfengine-Tutorial.ps", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(PFILE2, dvips, "cfengine-Reference.ps", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(HFILE1, makeinfo, "cfengine-Tutorial.html", "", \
  		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(HFILE2, makeinfo, "cfengine-Reference.html", "", \
  		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(IFILE1, makeinfo, "cfengine-Tutorial.info", "", \
  		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(IFILE2, makeinfo, "cfengine-Reference.info", "", \
  		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(DFILE3, latex, "paper1.dvi", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(PFILE3, dvips, "paper1.ps", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(DFILE4, latex, "paper2.dvi", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(PFILE4, dvips, "paper2.ps", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(DFILE5, latex, "locks.dvi", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)
AC_CHECK_PROG(PFILE5, dvips, "locks.ps", "", \
		$PATH:$prefix/bin:/usr/bin:/usr/local/bin)

DDOCS="$DFILE1 $DFILE2 $DFILE3 $DFILE4 $DFILE5"
AC_SUBST(DDOCS)
PDOCS="$PFILE1 $PFILE2 $PFILE3 $PFILE4 $PFILE5"
AC_SUBST(PDOCS)
HDOCS="$HFILE1 $HFILE2"
AC_SUBST(HDOCS)
IDOCS="$IFILE1 $IFILE2"
AC_SUBST(IDOCS)
CFDOCS="$DDOCS $PDOCS $HDOCS $IDOCS"
AC_SUBST(CFDOCS)

dnl ######################################################################
dnl Checks for libraries.
dnl ######################################################################


AC_CHECK_LIB(m,main)

AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt))
AC_CHECK_FUNC(gethostent, , AC_CHECK_LIB(nsl, gethostent))
AC_CHECK_FUNC(db_open,AC_DEFINE(HAVE_DB_OPEN), AC_CHECK_LIB(db, db_open))

AC_CHECK_LIB(wrap, main)           dnl TCP wrappers library
AC_CHECK_LIB(rx, main)             dnl GNU posix regex lib if you have regcomp in libc you shouldn't have this
AC_CHECK_LIB(crypto,main)


dnl ######################################################################
dnl Checks for header files.
dnl ######################################################################

AC_CHECK_HEADERS(unistd.h stdlib.h)
AC_CHECK_HEADERS(sys/mount.h)
AC_CHECK_HEADERS(utime.h)
AC_CHECK_HEADERS(time.h)
AC_CHECK_HEADERS(sys/time.h)
AC_CHECK_HEADERS(malloc.h)
AC_CHECK_HEADERS(vfs.h)
AC_CHECK_HEADERS(sys/vfs.h)
AC_CHECK_HEADERS(sys/sockio.h)
AC_CHECK_HEADERS(sys/statvfs.h)
AC_CHECK_HEADERS(sys/statfs.h)
AC_CHECK_HEADERS(sys/param.h)
AC_CHECK_HEADERS(fcntl.h)
AC_CHECK_HEADERS(sys/filesys.h)
AC_CHECK_HEADERS(dustat.h)
AC_CHECK_HEADERS(tcpd.h)
AC_CHECK_HEADERS(db_2.h db.h regex.h rxposix.h)
AC_CHECK_HEADERS(db_185.h)
AC_CHECK_HEADERS(db/db.h)
AC_CHECK_HEADERS(sys/systeminfo.h)
AC_CHECK_HEADERS(sys/acl.h)
AC_CHECK_HEADERS(openssl/des.h)

AC_HEADER_STDC
AC_HEADER_TIME
AC_HEADER_SYS_WAIT
AC_HEADER_DIRENT


dnl ######################################################################
dnl Checks for data types
dnl ######################################################################

AC_TYPE_MODE_T
AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_TYPE_PID_T

dnl ######################################################################
dnl Checks for typedefs, structures, and compiler characteristics.
dnl ######################################################################

AC_C_CONST

dnl ######################################################################
dnl Check for special functions
dnl ######################################################################

AC_CHECK_FUNCS(getcwd getnetgrent waitpid seteuid setegid setreuid setregid)
AC_CHECK_FUNCS(uname gethostname realpath regcomp chflags)
AC_CHECK_FUNCS(strstr putenv shmget)
AC_CHECK_FUNCS(bcopy mkfifo statfs statvfs)
AC_CHECK_FUNCS(sysinfo setsid strdup strrchr strerror)

dnl #######################################################################
dnl Newer BSD systems don't have a compatible rtentry - use ortentry
dnl #######################################################################
 
rtry=none
AC_MSG_CHECKING(for either struct rtentry or struct ortentry)
AC_EGREP_HEADER(rtentry, net/route.h, AC_DEFINE(HAVE_RTENTRY) rtry=rtentry)
AC_EGREP_HEADER(ortentry, net/route.h, AC_DEFINE(HAVE_ORTENTRY) rtry=ortentry)
AC_MSG_RESULT($rtry)

dnl #######################################################################
dnl Handle DCE + threading stuff
dnl #######################################################################



AC_CHECK_LIB(pthread,main)         dnl POSIX threads
AC_CHECK_HEADERS(pthread.h)
AC_CHECK_HEADERS(sched.h)

AC_CHECK_FUNC(lchown,AC_DEFINE(HAVE_LCHOWN))
AC_CHECK_FUNC(pthread_attr_setstacksize,AC_DEFINE(HAVE_PTHREAD_ATTR_SETSTACKSIZE),AC_CHECK_LIB(pthread, pthread_create))
AC_CHECK_FUNC(pthread_sigmask,AC_DEFINE(HAVE_PTHREAD_SIGMASK), AC_CHECK_LIB(pthread, pthread_create))

AC_ARG_ENABLE(DCE,[  --enable-DCE            support for DCE and ACLs],[
  case "$enableval" in
    no) 
        ;;
    *)
	AC_CHECK_LIB(thread, main) 
	AC_CHECK_LIB(dce, main)            dnl DFS
	AC_CHECK_HEADERS(dce/daclif.h)
	;;
  esac
])


dnl ######################################################################
dnl OS specific stuff
dnl ######################################################################

dnl
dnl dsm - To ensure conf.h is picked up via VPATH
dnl
if test "$srcdir" != "."; then
  CPPFLAGS="$CPPFLAGS -I`pwd`/src"
fi

dnl
dnl dsm - This case should be in the conf.h.in file.
dnl
case "$target_os" in

   sunos3*)
	AC_DEFINE(SUN3)
        ;;
   sunos4*)
	AC_DEFINE(SUN4)
        ;;
   solaris2.4*)
	AC_DEFINE(SOLARIS)
        AC_DEFINE(HAVE_SYS_ACL_H,)
        CPPFLAGS="-w $CPPFLAGS"
        LIBS="$LIBS -lelf"
        ;;
   solaris2.5*)
	AC_DEFINE(SOLARIS)
        AC_DEFINE(_POSIX_C_SOURCE)
        AC_DEFINE(__EXTENSIONS__)
        LIBS="$LIBS -lelf -lsec"
        ;;
   solaris2*)
	AC_DEFINE(SOLARIS)
        AC_DEFINE(__BIT_TYPES_DEFINED__) # avoid conflict with db.h
        AC_DEFINE(_POSIX_C_SOURCE)
        AC_DEFINE(__EXTENSIONS__)
        LIBS="$LIBS -lelf -lsec"
        ;;
   ultrix*)
	AC_DEFINE(ULTRIX)
        ;;
   hpux*|hp-ux*) 
	AC_DEFINE(HPuUX)

        if test "$GCC" != "yes"; then
	  AC_DEFINE(REGEX_MALLOC)
        fi
        LIBS="$LIBS -lc -lPW"
        ;;
   aix*)
	AC_DEFINE(AIX)
        ;;
   osf*)
	AC_DEFINE(OSF)
        ;;
   irix6*)
        AC_DEFINE(HAVE_ORTENTRY) # Have to hack this for 6.* owing to bug
	AC_DEFINE(IRIX)
        CFLAGS="$CFLAGS -w"
        ;;
   irix4*)
	AC_DEFINE(IRIX)
        CFLAGS="$CFLAGS -w"
        LIBS="$LIBS -lsun"
        ;;
   irix*)
	AC_DEFINE(IRIX)
        CFLAGS="$CFLAGS -w"
        ;;
   linux*)
	AC_DEFINE(LINUX)
        ;;
   freebsd*)
	AC_DEFINE(FREEBSD)
        ;;
   netbsd*)
	AC_DEFINE(NETBSD)
        ;;
   newsos*)
	AC_DEFINE(NEWS_OS)
        ;;
   bsd/os*)
	AC_DEFINE(BSDOS)
        ;;
   bsd*)
	AC_DEFINE(BSD43)
        ;;
   aos*)
	AC_DEFINE(AOS)
        ;;
   nextstep*)
	AC_DEFINE(NEXTSTEP)
        ;;

   unicos*)
	AC_DEFINE(CFCRAY)
        ;;

   cray*)
	AC_DEFINE(CFCRAY)
        ;;

   openbsd*)
	AC_DEFINE(OPENBSD)
        ;;

   gnu*)
	AC_DEFINE(CFGNU)
        ;;

   sysv4.2MP|unix_sv*)
	AC_DEFINE(UNIXWARE)
        ;;

   cygwin32*)
        AC_DEFINE(NT)
        ;;

   sco*)
        AC_DEFINE(SCO)
        ;;

   *)
	AC_MSG_ERROR(Unknown system type $target_os)
        ;;
esac

LIBS="$LIBS -L../pub -lcfpub"

dnl #####################################################################
dnl This needed to be configurable, so now it is. -Doug
dnl Fixed up the format - dsm
dnl #####################################################################

AC_ARG_WITH(LOCKDIR,
	[  --with-lockdir=LOCKDIR  directory where lockfiles should go ],
	[
		if test x$withval != x ; then
			LOCKDIR=$withval
		else
			if test -d /var/run ; then
				LOCKDIR=/var/run/cfengine
			else
				LOCKDIR=/etc/cfengine
			fi
		fi
		AC_DEFINE_UNQUOTED(LOCKFILEDIR, "${LOCKDIR}")
	],
	[
		if test -d /var/run ; then
			LOCKDIR=/var/run/cfengine
		else
			LOCKDIR=/etc/cfengine
		fi
		AC_DEFINE_UNQUOTED(LOCKFILEDIR, "${LOCKDIR}")
	]
)

AC_ARG_WITH(LOGDIR,
	[  --with-logdir=LOGDIR  directory where logfiles should go ],
	[
		if test x$withval != x ; then
			LOGDIR=$withval
		else
			if test -d /var/run; then
				LOGDIR=/var/run/cfengine
			else
				LOGDIR=/etc/cfengine
			fi
		fi
		AC_DEFINE_UNQUOTED(LOGFILEDIR, "${LOGDIR}")
	],
	[
		if test -d /var/run; then
			LOGDIR=/var/run/cfengine
		else
			LOGDIR=/etc/cfengine
		fi
		AC_DEFINE_UNQUOTED(LOGFILEDIR, "${LOGDIR}")
	]
)
  
dnl #####################################################################
dnl Fix for lex/flex
dnl #####################################################################

AC_MSG_CHECKING("8-bit support in Flex")

if test "$LEX" = "flex"; then
   EIGHTBIT="\200-\377"
   AC_SUBST(EIGHTBIT)
   AC_MSG_RESULT("8-bit support added")
else
   AC_MSG_RESULT("no 8-bit support")
fi
  


dnl #####################################################################
dnl Hostname and Version stuff
dnl #####################################################################
  
AC_PATH_PROG(HOSTNAME, hostname, "", $PATH)

dnl #####################################################################
dnl dsm - is this really necessary? mb - yes for ancient 4.3
dnl #####################################################################

AC_DEFINE_UNQUOTED(AUTOCONF_HOSTNAME, "`$HOSTNAME`")
AC_DEFINE_UNQUOTED(AUTOCONF_SYSNAME, "$target_os")
AC_DEFINE_UNQUOTED(CFVERSION, "$cfversion")

dnl ######################################################################
dnl Now make the Makefiles
dnl ######################################################################

AC_OUTPUT(pub/Makefile doc/Makefile src/Makefile bin/Makefile contrib/Makefile \
	inputs/Makefile Makefile bin/cf-install doc/cfengine.8 \
	bin/cfmail bin/cfdaily bin/cfwrap contrib/cfdoc src/cflex.l)

chmod 0755 bin/cfmail bin/cfdaily bin/cfwrap 

AC_MSG_RESULT("Configuration done. Run make to build cfengine.")

